package fem2;

import inf.jlinalg.IVector;
import inf.jlinalg.SolveFailedException;
import inf.text.ArrayFormat;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import junit.framework.Assert;
import math2.FunctionRnToR;
import math2.RampFunctionRToR;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import fem2.analysis.Analysis;
import fem2.analysis.LinearStaticAnalysis;
import fem2.element.BBarStructuralElement;
import fem2.element.StructuralElement;
import fem2.enu.EchoLevelType;
import fem2.enu.State;
import fem2.gui.Viewer;
import fem2.material.StVenantKirchhoffMM;
import fem2.strategies.ArcLengthSolutionStrategyWithSphereConstraint;

@RunWith(Parameterized.class)
public class PatchTestT3 {

	@Parameters
	public static Collection<Object[]> parameters() {
		List<Object[]> params = new LinkedList<Object[]>();

		params.add(new Object[] { 0 });
		params.add(new Object[] { 1 });
		return params;
	}

	private int option;

	/**
	 * 
	 * @param option
	 * <br>
	 *            option=0: use linear StructuralElement<br>
	 *            option=1: use BBarStructuralElement
	 */
	public PatchTestT3(int option) {
		this.option = option;
	}

	public Mesh createMesh() {
		return createMesh(Math.random(), Math.random());
	}

	public Mesh createMesh(double rx, double ry) {
		/*
		 * mesh
		 */
		Mesh mesh = new Mesh();

		double l = 1;
		double h = 1;

		Debugger.watch("rx = " + rx);
		Debugger.watch("ry = " + ry);

		Node n1 = mesh.addNode(new Node(0, 0));
		Node n2 = mesh.addNode(new Node(l, 0));
		Node n3 = mesh.addNode(new Node(l, h));
		Node n4 = mesh.addNode(new Node(0, h));
		Node n5 = mesh.addNode(new Node(rx * l, ry * h));

		mesh.addFace(new Face(n1, n2, n5));
		mesh.addFace(new Face(n2, n3, n5));
		mesh.addFace(new Face(n3, n4, n5));
		mesh.addFace(new Face(n4, n1, n5));

		mesh.addEdge(new Edge(n2, n3));

		mesh.setNumberOfFields(2);

		/*
		 * constraint
		 */
		Constraint c1 = new Constraint(false, false);
		Constraint c2 = new Constraint(true, false);
		Constraint c3 = new Constraint(false, true);
		n1.setConstraint(c1);
		n2.setConstraint(c2);
		n4.setConstraint(c3);

		return mesh;
	}

	public Model createModel() {
		Mesh mesh = createMesh();

		double E = 2;
		double nu = 0.3;
		double t = 1.0;
		State ss = State.PLANE_STRESS;
		MaterialModel mm = new StVenantKirchhoffMM(E, nu, t, 0, ss);

		Model m = new Model(mesh);
		for (int i = 0; i < mesh.countFaces(); i++) {
			Element e;

			switch (option) {
			case 0:
				e = new StructuralElement(mesh.getFace(i), mm);
				break;
			case 1:
				e = new BBarStructuralElement(mesh.getFace(i), mm);
				break;
			default:
				throw new Error("invalid option");
			}

			m.addElement(e);
		}

		return m;
	}

	@Test
	public void testSolve1() throws SolveFailedException {
		double F = 0.5;
		Model m = createModel();
		Force l1 = new Force(m.getMesh().getNode(1), F, 0);
		Force l2 = new Force(m.getMesh().getNode(2), F, 0);
		m.addLoad(l1);
		m.addLoad(l2);

		Analysis an = new LinearStaticAnalysis(m);
		an.run();

		/*
		 * print the displacement and check with theoretical values
		 */
		System.out.println("testSolve1 results:");
		double[][] Uh = new double[][] { { 0, 0 }, { 0.5, 0 }, { 0.5, -0.15 }, { 0, -0.15 } };
		for (int i = 0; i < 4; i++) {
			double[] uHatLocal = m.getMesh().getNode(i).getUHatLocal();
			System.out.println(ArrayFormat.format(uHatLocal));
			Assert.assertEquals(Uh[i][0], uHatLocal[0], 1e-10);
			Assert.assertEquals(Uh[i][1], uHatLocal[1], 1e-10);
		}

		for (int i = 0; i < m.countElements(); i++) {
			StructuralElement e = (StructuralElement) m.getElement(i);
			for (int j = 0; j < e.getNumIntegrationPoints(); j++) {
				IVector sigma = e.getStress(j);
				Assert.assertEquals(1.0, sigma.get(0), 1e-10);
				Assert.assertEquals(0.0, sigma.get(1), 1e-10);
				Assert.assertEquals(0.0, sigma.get(2), 1e-10);
			}
		}
	}

	@Test
	public void testSolve2() throws SolveFailedException {
		double F = 1;
		Model m = createModel();
		Mesh mesh = m.getMesh();

		FunctionRnToR ld = new RampFunctionRToR(-1, 1, F, F);
		Load el = new DistributedLoad(mesh.getEdge(0), ld, null);
		m.addLoad(el);

		double[] re = el.computeRe(0.0);
		System.out.println("re =" + ArrayFormat.format(re));

		Analysis an = new LinearStaticAnalysis(m);
		an.run();

		/*
		 * print the displacement and check with theoretical values
		 */
		System.out.println("testSolve2 results:");
		double[][] Uh = new double[][] { { 0, 0 }, { 0.5, 0 }, { 0.5, -0.15 }, { 0, -0.15 } };
		for (int i = 0; i < 4; i++) {
			double[] uHatLocal = m.getMesh().getNode(i).getUHatLocal();
			System.out.println(ArrayFormat.format(uHatLocal));
		}
		for (int i = 0; i < 4; i++) {
			double[] uHatLocal = m.getMesh().getNode(i).getUHatLocal();
			Assert.assertEquals(Uh[i][0], uHatLocal[0], 1e-10);
			Assert.assertEquals(Uh[i][1], uHatLocal[1], 1e-10);
		}

		for (int i = 0; i < m.countElements(); i++) {
			StructuralElement e = (StructuralElement) m.getElement(i);
			for (int j = 0; j < e.getNumIntegrationPoints(); j++) {
				IVector sigma = e.getStress(j);
				Assert.assertEquals(1.0, sigma.get(0), 1e-10);
				Assert.assertEquals(0.0, sigma.get(1), 1e-10);
				Assert.assertEquals(0.0, sigma.get(2), 1e-10);
			}
		}
	}

	@Test
	public void testSolve3() throws SolveFailedException {
		Model m = createModel();
		Mesh mesh = m.getMesh();

		double du = 0.5;
		Constraint c1 = new Constraint(false, false);
		c1.setValue(0, du);
		mesh.getNode(1).setConstraint(c1);
		Constraint c2 = new Constraint(false, true);
		c2.setValue(0, du);
		mesh.getNode(2).setConstraint(c2);

		Analysis an = new LinearStaticAnalysis(m);
		an.run();

		/*
		 * print the displacement and check with theoretical values
		 */
		System.out.println("testSolve3 results:");
		double[][] Uh = new double[][] { { 0, 0 }, { 0.5, 0 }, { 0.5, -0.15 }, { 0, -0.15 } };
		for (int i = 0; i < 4; i++) {
			double[] uHatLocal = m.getMesh().getNode(i).getUHatLocal();
			System.out.println(ArrayFormat.format(uHatLocal));
			Assert.assertEquals(Uh[i][0], uHatLocal[0], 1e-10);
			Assert.assertEquals(Uh[i][1], uHatLocal[1], 1e-10);
		}

		for (int i = 0; i < m.countElements(); i++) {
			StructuralElement e = (StructuralElement) m.getElement(i);
			for (int j = 0; j < e.getNumIntegrationPoints(); j++) {
				IVector sigma = e.getStress(j);
				Assert.assertEquals(1.0, sigma.get(0), 1e-10);
				Assert.assertEquals(0.0, sigma.get(1), 1e-10);
				Assert.assertEquals(0.0, sigma.get(2), 1e-10);
			}
		}
	}

	@Test
	public void testSolve4() throws SolveFailedException {
		double F = 0.5;
		Model m = createModel();
		Force l1 = new Force(m.getMesh().getNode(1), F, 0);
		Force l2 = new Force(m.getMesh().getNode(2), F, 0);
		m.addLoad(l1);
		m.addLoad(l2);

		Analysis an = new Analysis(m);

		an.setStrategy(new ArcLengthSolutionStrategyWithSphereConstraint(m, 0.1, 0.0));
		an.getStrategy().setEchoLevel(EchoLevelType.DEBUG, EchoLevelType.OUTPUT);
		an.getStrategy().setNumberOfTimeStep(10);

		an.run();

		/*
		 * print the displacement and check with theoretical values
		 */
		System.out.println("testSolve4 results:");
		// double[][] Uh = new double[][] { { 0, 0 }, { 0.5, 0 }, { 0.5, -0.15
		// }, { 0, -0.15 } };
		for (int i = 0; i < 4; i++) {
			double[] uHatLocal = m.getMesh().getNode(i).getUHatLocal();
			System.out.println(ArrayFormat.format(uHatLocal));
			// Assert.assertEquals(Uh[i][0], uHatLocal[0], 1e-10);
			// Assert.assertEquals(Uh[i][1], uHatLocal[1], 1e-10);
		}
	}

	public static void main(String[] args) throws SolveFailedException {
		Model m = new PatchTestT3(0).createModel();
		Viewer v = new Viewer(m);
		v.setVisible(true);
	}
}
